package com.qb.common.utils

import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPStaticUtils
import com.blankj.utilcode.util.ToastUtils
import com.qb.common.bean.ImageBean
import com.qb.common.bean.MyImageBean
import com.qb.common.bean.UserInfo
import com.qb.common.constant.Constants
import org.litepal.LitePal

object LitePalUtils {
    fun initAddUser(userInfo: UserInfo){
        var dbUser = LitePal.where("username=?",userInfo.username).findFirst(UserInfo::class.java)
        if (ValidUtils.isValid(dbUser)){
            return
        }
        var result = userInfo.save()
    }
    fun initUser(userInfo: UserInfo){
        var dbUser = LitePal.where("username=?",userInfo.username).findFirst(UserInfo::class.java)
        if (ValidUtils.isValid(dbUser)){
            ToastUtils.showShort("该用户已存在")
            return
        }
        var result = userInfo.save()
    }
    fun saveUser(userInfo: UserInfo){
        var dbUser = LitePal.where("username=?",userInfo.username).findFirst(UserInfo::class.java)
        if (ValidUtils.isValid(dbUser)){
            ToastUtils.showShort("该用户已存在")
            return
        }
        var result = userInfo.save()
        if (result){
            SPStaticUtils.put(Constants.LOGININFO, GsonUtils.toJson(userInfo))
            ToastUtils.showShort("账号注册成功")
        }else{
            ToastUtils.showShort("账号注册失败")
        }
    }
    fun findUser(username: String): UserInfo? {
        return LitePal.where("username=?", username).findFirst(UserInfo::class.java)
    }
    fun deleteUser(username: String): Int {
        return LitePal.deleteAll(UserInfo::class.java, "username=?", username)
    }
    fun myLike(imageBean: ImageBean){
        var myImageBeanDb = LitePal.where("img=? and isLike=?",imageBean.img,"1").findFirst(MyImageBean::class.java)
        if (ValidUtils.isValid(myImageBeanDb)){
            ToastUtils.showShort("您已添加好评")
            return
        }
        var myImageBean = MyImageBean()
        myImageBean.title = if (ValidUtils.isValid(imageBean.title)) imageBean.title else "未知"
        myImageBean.img = if (ValidUtils.isValid(imageBean.img)) imageBean.img else "http://www.baidu"
        myImageBean.from = imageBean.from
        myImageBean.good = imageBean.good
        myImageBean.bad = imageBean.bad
        myImageBean.isLike = 1
        var result = myImageBean.save()
        if (result){
            ToastUtils.showShort("添加好评成功")
        }else{
            ToastUtils.showShort("添加好评失败")
        }
    }

    fun myBad(imageBean: ImageBean){
        var myImageBeanDb = LitePal.where("img=? and isLike=?",imageBean.img,"0").findFirst(MyImageBean::class.java)
        if (ValidUtils.isValid(myImageBeanDb)){
            ToastUtils.showShort("您已差评")
            return
        }
        var myImageBean = MyImageBean()
        myImageBean.title = imageBean.title
        myImageBean.img = imageBean.img
        myImageBean.from = imageBean.from
        myImageBean.good = imageBean.good
        myImageBean.bad = imageBean.bad
        myImageBean.isLike = 0
        var result = myImageBean.save()
        if (result){
            ToastUtils.showShort("添加差评成功")
        }else{
            ToastUtils.showShort("添加差评失败")
        }
    }
    fun queryLikeOrBad(page:Int=1,isLike:Int = 0,pageSize:Int = 10):ArrayList<MyImageBean>{
        var myImageBeans = LitePal.where("isLike=?","$isLike")
            .order("id DESC") // 根据 good 字段降序排列
            .offset((page - 1) * pageSize) // 设置分页限制
            .limit(pageSize) // 设置分页限制
            .find(MyImageBean::class.java)
        LogUtils.e("输出所有查询结果：$myImageBeans")
        return myImageBeans as ArrayList<MyImageBean>
    }
}